package org.example.bigwork.service.impl;

import org.example.bigwork.entity.Student;
import org.example.bigwork.entity.StudentTask;
import org.example.bigwork.entity.Task;
import org.example.bigwork.mapper.StudentMapper;
import org.example.bigwork.mapper.StudentTaskMapper;
import org.example.bigwork.mapper.TaskMapper;
import org.example.bigwork.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private TaskMapper taskMapper;
    
    @Autowired
    private StudentTaskMapper studentTaskMapper;

    @Override
    public Student getStudentById(String studentId) {
        // 获取学生基本信息
        Student student = studentMapper.findById(studentId);
        if (student != null) {
            // 获取学生的任务列表并设置到学生对象中
            List<Task> tasks = studentTaskMapper.findTasksByStudentId(studentId);
            student.setTasks(tasks);
        }
        return student;
    }

    @Override
    public List<Task> getStudentTasks(String studentId) {
        // 获取学生的所有任务，按截止时间排序
        return studentTaskMapper.findTasksByStudentId(studentId);
    }

    @Override
    @Transactional
    public boolean claimTask(String studentId, String taskId) {
        // 检查任务是否存在
        Task task = taskMapper.findById(taskId);
        if (task == null) {
            return false;
        }
        
        // 检查任务是否已被领用
        if ("已领用".equals(task.getClaimStatus())) {
            return false;
        }
        
        // 检查时间合法性
        if (task.getStartTime() != null && task.getEndTime() != null) {
            if (task.getStartTime().isAfter(task.getEndTime())) {
                return false;
            }
        }
        
        // 创建学生任务关联
        StudentTask studentTask = StudentTask.builder()
                .studentId(studentId)
                .taskId(taskId)
                .claimTime(LocalDateTime.now())
                .build();
        
        // 保存学生任务关联
        studentTaskMapper.insert(studentTask);
        
        // 更新任务状态为已领用
        taskMapper.updateClaimStatus(taskId, "已领用");
        
        return true;
    }

    @Override
    @Transactional
    public boolean cancelTask(String studentId, String taskId) {
        // 检查学生任务关联是否存在
        StudentTask studentTask = studentTaskMapper.findByStudentIdAndTaskId(studentId, taskId);
        if (studentTask == null) {
            return false;
        }
        
        // 删除学生任务关联
        studentTaskMapper.delete(studentId, taskId);
        
        // 更新任务状态为未领用
        taskMapper.updateClaimStatus(taskId, "未领用");
        
        return true;
    }

    @Override
    public Map<String, Task> getTaskMap(String studentId) {
        // 获取学生的所有任务
        List<Task> tasks = studentTaskMapper.findTasksByStudentId(studentId);
        
        // 将任务列表转换为Map，实现O(1)的查找效率
        return tasks.stream().collect(Collectors.toMap(Task::getTaskId, task -> task));
    }
} 